package com.haochen.generation.controller;

import cn.hutool.json.JSONUtil;
import com.haochen.generation.domain.Leveling;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@RequestMapping("/test")
@RestController
public class TestController {

    /**
     * 查询留言列表
     */
    @GetMapping("/syncjson")
    public Object syncjson(Leveling leveling) {

        final String CHARSET_NAME = "UTF-8";

        String fileName = "C:\\code\\scr_serverT\\relation-api-starter\\src\\main\\resources\\china_area_data.json";
        String outpath = "C:\\code\\sheng.csv";


        List<String> content = readFile(fileName, CHARSET_NAME);

        String json = StringUtils.join(content, "");

        Map<String, Map<String, String>> dataMap = JSONUtil.toBean(json, Map.class);

        System.out.println(dataMap.keySet());

        Map<String, String> sheng = dataMap.get("86");
        List<List<String>> out = new ArrayList<>();

        AtomicReference<Integer> max = new AtomicReference<>(0);
        sheng.forEach((k, v) -> {
            List<String> temp = new ArrayList<>();
            temp.add(v);

            Map<String, String> tempMap = dataMap.get(k);
            temp.addAll(new ArrayList<>(tempMap.values()).stream().sorted().collect(Collectors.toList()));

            if (max.get() < temp.size()) {
                max.set(tempMap.size());
            }

            out.add(temp);
        });

        for (int i = 0; i < max.get(); i++) {
            StringBuilder outLine = new StringBuilder();
            for (List<String> tempList : out) {
                if (tempList.size() < i + 1) {
                    outLine.append(",");
                } else {
                    outLine.append(tempList.get(i)).append(",");
                }
            }
//                outLine.append("\n");
            writeToFile(outpath, outLine);
        }


        return "";
    }

    private static void writeToFile(String fileName, StringBuilder outLine) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) { // 设置参数为true以启用追加模式
            writer.write(outLine.toString());
            writer.newLine(); // 可选的，用于在每次追加后插入一个换行符
            writer.flush(); // 刷新缓冲区
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static List<String> readFile(String fileName, String CHARSET_NAME) {
        List<String> content = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), CHARSET_NAME))) {
            String line;
            while ((line = br.readLine()) != null) {
                content.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }
}
